package com.kx.yooli.kx.wechat.utils.comm;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/2
 * PROJECT_NAME wechat
 * description create GsonUtils by john_sqwen
 * and googleJson value is
 */
public class GsonUtils {
    private  static final Logger log = LoggerFactory.getLogger(GsonUtils.class);
    private static Gson gson = null;
    static {
        gson = new Gson();
    }
    private GsonUtils() {
    }
    /**
     * object change json
     * @param  object value is object
     * @return json
     */
    public static String gsonString(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }
    /**
     * string to jsonObject
     * @author john sqwen
     * @param  str
     * @return jsonObject
     */
    public static  JsonObject strJsonObject(String str){
        log.info("param value is {}",str);
        JsonElement jsonElement = JsonParser.parseString(str);
        return jsonElement.getAsJsonObject();
    }
    /**
     *object to json string
     * @param obj in the method
     * @return string jsonStr
     */
    public static String toJson(Object obj) {
        Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .create();
        return gson.toJson(obj);
    }
    /**
     * Json change object
     *
     * @param gsonString in thisString
     * @param cls to after change class
     * @return object
     */
    public static <T> T gsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }
    /**
     * json change to list<T>
     *
     * @param gsonString in this method
     * @param cls after change class
     * @return list<T>
     */
    public static <T> List<T> gsonToList(String gsonString, Class<T> cls) {
        List<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
        }
        return list;
    }

    /**
     * json change to list contains map
     *
     * @param gsonString in this method
     * @return List<Map<String, T>>
     */
    public static <T> List<Map<String, T>> gsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
            }.getType());
        }
        return list;
    }

    /**
     * json change map
     *
     * @param gsonString in the method
     * @return Map<String, T>
     */
    public static <T> Map<String, T> gsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }
}
